//
//  TTScrollMenusView.m
//  TTScrollMenus-Demo
//
//  Created by 赵国腾 on 16/5/16.
//  Copyright © 2016年 赵国腾. All rights reserved.
//

#import "TTScrollMenusView.h"
#import "TTMenuLayout.h"
#import "TTMenuCell.h"

static NSString *itemIdentifier = @"TTMenuCell";

@interface TTScrollMenusView ()<UICollectionViewDelegate, UICollectionViewDataSource>

/** 集合视图 */
@property (nonatomic, strong) UICollectionView *collectionView;

/** 分页指示视图 */
@property (nonatomic, strong) UIPageControl *pageControl;

/** 自定义布局 */
@property (nonatomic, strong) TTMenuLayout *layout;

@end

@implementation TTScrollMenusView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
    }
    return self;
}

- (void)awakeFromNib {
    [self setup];
}

- (void)setup {
    
    self.layout = [[TTMenuLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    
    // 注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"TTMenuCell" bundle:nil] forCellWithReuseIdentifier:itemIdentifier];
    
    // 添加分页视图
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
    [self.pageControl setCurrentPageIndicatorTintColor:[UIColor darkGrayColor]];
    [self.pageControl setPageIndicatorTintColor:[UIColor lightGrayColor]];
    self.pageControl.hidesForSinglePage = YES;
    
    [self addSubview:self.pageControl];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat selfW = CGRectGetWidth(self.bounds);
    CGFloat selfH = CGRectGetHeight(self.bounds);
    
    self.collectionView.frame = self.bounds;
    
    // 分页视图的位置
    CGFloat pageControlH = 20.0f;
    self.pageControl.frame = CGRectMake(0, selfH - pageControlH, selfW, pageControlH);
}

#pragma mark - 属性设置

- (void)setLineCount:(NSInteger)lineCount {
    
    _lineCount = lineCount;
    self.layout.lineCount = lineCount;
    [self setPageNumbersWithCount:self.dataList.count];
}

- (void)setColuCount:(NSInteger)coluCount {
    
    _coluCount = coluCount;
    self.layout.coluCount = coluCount;
    [self setPageNumbersWithCount:self.dataList.count];
}

- (void)setEdgeInsets:(UIEdgeInsets)edgeInsets {
    _edgeInsets = edgeInsets;
    self.layout.edgeInsets = edgeInsets;
}

- (void)setDataList:(NSArray *)dataList {
    
    _dataList = dataList;
    
    [self setPageNumbersWithCount:dataList.count];
    [self.collectionView reloadData];
}

- (void)setCurrentpageColor:(UIColor *)currentpageColor {
    
    _currentpageColor = currentpageColor;
    [self.pageControl setCurrentPageIndicatorTintColor:currentpageColor];
}

- (void)setPageColor:(UIColor *)pageColor {
    _pageColor = pageColor;
    [self.pageControl setPageIndicatorTintColor:pageColor];
}

// 这是分页的个数
- (void)setPageNumbersWithCount:(NSInteger)count {
    NSInteger pages = (NSInteger)ceilf(count / (self.layout.lineCount * self.layout.coluCount * 1.0));
    self.pageControl.numberOfPages = pages;
}

#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TTMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath];
    cell.menu = self.dataList[indexPath.item];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(scrollMenusView:didSelectIndex:)]) {
        [self.delegate scrollMenusView:self didSelectIndex:indexPath.item];
    }
}

#pragma mark - 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = (NSInteger)scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
}

@end
